package com.interesting.client.support.video;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.SeekBar;

public class MusicPlayer implements OnCompletionListener, OnPreparedListener, OnBufferingUpdateListener,
		OnErrorListener {

	public MediaPlayer musicPlayer;
	private SeekBar skbProgress;
	private Timer mTimer = new Timer();
	private Context context;
	private String tag = "MusicPlayer";
	boolean reset = false, pause = false, stop = false, completion = false;

	public MusicPlayer(SeekBar skbProgress) {
		this.skbProgress = skbProgress;

		musicPlayer = new MediaPlayer();
		musicPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		musicPlayer.setOnPreparedListener(this);
		musicPlayer.setOnBufferingUpdateListener(this);
		musicPlayer.setOnCompletionListener(this);
		mTimer.schedule(mTimerTask, 0, 500);// TODO?
	}

	TimerTask mTimerTask = new TimerTask() { // TODO?

		@Override
		public void run() {
			if (musicPlayer == null) {
				return;
			}
			if (musicPlayer.isPlaying() && skbProgress.isPressed() == false) {
				handleProgress.sendEmptyMessage(0);
			}
		}
	};

	// 进度条
	Handler handleProgress = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			int position = musicPlayer.getCurrentPosition();
			int duration = musicPlayer.getDuration();

			if (duration > 0) {
				long pos = skbProgress.getMax() * position / duration;
				skbProgress.setProgress((int) pos);
			}
		}

	};

	public void playUri(Context context, String uri) {
		this.context = context;
		try {
			musicPlayer.reset();
			musicPlayer.setDataSource(context, Uri.parse(uri));
			musicPlayer.prepareAsync();//
			reset = true;
		} catch (IllegalArgumentException e) {
			Log.d(tag, "IllegalArgumentException");
			e.printStackTrace();
		} catch (IllegalStateException e) {
			Log.d(tag, "IllegalStateException");

			e.printStackTrace();
		} catch (IOException e) {
			Log.d(tag, "IOException");

			e.printStackTrace();
		}

	}

	public void stop() {
		if (musicPlayer != null) {
			musicPlayer.stop();
			pause = false;
			stop = true;
			musicPlayer.release();
			musicPlayer = null;
		}
	}

	public void start() {
		musicPlayer.start();
		pause = false;
	}

	public void pause() {
		musicPlayer.pause();
		pause = true;
	}

	public boolean isPaused() {

		return pause;
	}

	public boolean isPlaying() {
		if (musicPlayer.isPlaying())
			return true;
		else
			return false;
	}

	public int getDuration() {
		return musicPlayer.getDuration();
	}

	public void seekTo(int msec) {
		musicPlayer.seekTo(msec);
	}

	@Override
	public void onPrepared(MediaPlayer arg0) {
		arg0.start();
		Log.d("MusicPlayer", "onprepared");
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		Log.d("MusicPlayer", "oncompletion");
		musicPlayer.release();
	}

	@Override
	public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
		Log.d("MusicPlayer", "onbufferingupdate");
		skbProgress.setSecondaryProgress(bufferingProgress);
		int currentProgress = skbProgress.getMax() * musicPlayer.getCurrentPosition() / musicPlayer.getDuration();
		Log.e(currentProgress + "% play", bufferingProgress + "% buffer");

	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		Log.d(tag, mp.getDuration() + what + extra + "");
		return false;
	}

}
